package com.ruoyi.app.controller.member.controller;

import com.ruoyi.app.controller.marketing.entity.MemberCouponSearchVO;
import com.ruoyi.app.controller.marketing.entity.MemberCouponVO;
import com.ruoyi.app.controller.marketing.service.ICouponService;
import com.ruoyi.app.controller.member.entity.BO.MemberCouponReceiveBO;
import com.ruoyi.app.controller.member.service.IMemberCouponService;
import com.ruoyi.app.framework.base.BaseAppController;
import com.ruoyi.common.annotation.AuthMember;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/memberCoupon")
@Api(value = "会员优惠券", description = "会员优惠券模块接口")
public class MemberCouponController extends BaseAppController {

    @Resource
    private IMemberCouponService memberCouponService;

    @Resource
    private ICouponService couponService;

    @ApiOperation(value = "领取优惠券", notes = "领券中心领取优惠券使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权密钥", required = true, dataTypeClass = String.class,paramType = "header"),
            @ApiImplicitParam(name = "couponId", value = "优惠券ID", required = false, dataTypeClass = Long.class,paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 500, message = "登录超时！")
    })
    @GetMapping("/receive")
    public AjaxResult receive(@AuthMember MemberDO member) {
        //TODO:判断优惠券是否符合领取条件
        if (StringUtils.isEmpty(ServletUtils.getParameter("couponId"))) {
            throw new BusinessException("请先选择优惠券！");
        }
        MemberCouponReceiveBO receiveBO = new MemberCouponReceiveBO();
        receiveBO.setMemberId(member.getId());
        receiveBO.setReceiveNum(1);
        receiveBO.setActivityId(0);//领券中心领取
        memberCouponService.receive(receiveBO);
        return AjaxResult.success("领取成功!");
    }

//    @ApiOperation(value = "获取已领取优惠券详情", notes = "我的优惠券列表点击查看的详情")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "token", value = "授权密钥", required = true, paramType = "header"),
//            @ApiImplicitParam(name = "memberCouponId", value = "领取优惠券ID", required = true, paramType = "query"),
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, message = ""),
//            @ApiResponse(code = 500, message = "登录超时！")
//    })
//    @GetMapping("/receiveDetail")
//    public AjaxResult getDetail(@ApiIgnore @AuthMember MemberDO member) {
//        CouponSearchVO couponSearchVO = new CouponSearchVO();
//        couponSearchVO.setShopId(member.getShopId());
//        couponSearchVO.setIsDelete(0);
//        couponSearchVO.setIsEnable(1);
//        ProductVO product;
//        if (StringUtils.isNotEmpty(ServletUtils.getParameter("memberCouponId"))) {
//            couponSearchVO.setId(Long.parseLong(ServletUtils.getParameter("memberCouponId")));
//            product = productService.getDetail(productSearchVO);
//        }
//        if (product.getIsVip() != null && product.getIsVip() == 1) {
//            if (member.getId() == null || member.getId() == 0) {
//                product.setIsAbleView(0);
//            } else {
//                MemberVO memberInfo = memberService.getSimpleInfoById(member.getId());
//                if (memberInfo.getIsVip() == 0) {
//                    product.setIsAbleView(0);
//                }
//            }
//        }
//        List<ProductSkuStockDO> skuStockList = productService.searchSkuStockList(productSearchVO);
//        product.setSkuList(skuStockList);
//        return AjaxResult.success(product);
//    }

    @ApiOperation(value = "我的优惠券列表", notes = "查询我的优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权密钥", required = true, dataTypeClass = String.class,paramType = "header"),
            @ApiImplicitParam(name = "memberCouponStatus", value = "优惠券状态（0未使用 1已使用 2已过期）", dataTypeClass = Integer.class,required = false, paramType = "query"),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型（1代金券 2折扣卡）", required = false, dataTypeClass = Integer.class,paramType = "query"),
            @ApiImplicitParam(name = "start", value = "页码（不传默认1）", required = false,dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "limits", value = "每页条数（不传默认1000）", required = false, dataTypeClass = Integer.class,paramType = "query"),
            @ApiImplicitParam(name = "orderBy", value = "排序", required = false,dataTypeClass = Integer.class, paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ""),
            @ApiResponse(code = 500, message = "登录超时！")
    })
    @GetMapping("/searchList")
    public TableDataInfo searchList(@AuthMember MemberDO member) {
        MemberCouponSearchVO couponSearchVO = new MemberCouponSearchVO();
        couponSearchVO.setMemberId(member.getId());
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("memberCouponStatus"))) {
            couponSearchVO.setMemberCouponStatus(Integer.valueOf(ServletUtils.getParameter("memberCouponStatus")));
        }
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("couponType"))) {
            couponSearchVO.setCouponType(Integer.valueOf(ServletUtils.getParameter("couponType")));
        }
        couponSearchVO.setModuleType(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        couponSearchVO.setIsEnable(1);
        couponSearchVO.setIsDelete(0);
        HashMap<String, Object> params = new HashMap<>();
        params.put("start", ServletUtils.getParameterToInt("start", 1));
        params.put("limits", ServletUtils.getParameterToInt("limits", 200));
        BaseEntity pageVo = new BaseEntity();
        pageVo.setParams(params);
        startPage(pageVo);
        List<MemberCouponVO> couponList = memberCouponService.searchList(couponSearchVO);
        TableDataInfo returnData = getDataTable(couponList);
        HashMap<String, Object> appendData = new HashMap<>();
        appendData.put("memberCouponCount",memberCouponService.searchMemberCouponStatusCount(member.getId()));
        returnData.setAppendData(appendData);
        return returnData;
    }

//    @ApiOperation(value = "可用优惠券列表", notes = "查询订单可用的优惠券列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "token", value = "授权密钥", required = true, paramType = "header"),
//            @ApiImplicitParam(name = "useStatus", value = "使用状态", required = false, paramType = "query"),
//            @ApiImplicitParam(name = "start", value = "页码（不传默认1）", required = false, paramType = "query"),
//            @ApiImplicitParam(name = "limits", value = "每页条数（不传默认1000）", required = false, paramType = "query"),
//            @ApiImplicitParam(name = "orderBy", value = "排序", required = false, paramType = "query"),
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, message = ""),
//            @ApiResponse(code = 500, message = "登录超时！")
//    })
//    @GetMapping("/ableUseList")
//    public ResultData footCoupon(@PathVariable("num") Integer num, @PathVariable("type") Integer type, @AuthMember MemberDO member) {
//        ShowCouponVO showCouponVO = ICouponService.footCoupon(num, member.getId(), type);
//        return new ResultData(0, "查询成功", showCouponVO);
//    }


//    /**
//     * 结算页面计算多少条优惠可用
//     */
//    @GetMapping("/countCoupon/{modelType}")
//    public ResultData countCoupon(@PathVariable("modelType") Integer modelType, @AuthMember MemberDO member) {
//        int count  = ICouponService.countCoupon(modelType, member.getId());
//        return ResultData.success(count);
//    }

}
